<template>
	<view>
		<pickerModule :picker="item" v-for="(item, index) in picker" :key="index" @pickerChange="pickerChange"></pickerModule>
		<baseForm :column.sync="column" @submitFun="submitFun" :submitStatus="submitDisable"></baseForm>
	</view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import pickerModule from '@/wxcomponents/form/picker.vue';
import { format, dectList } from '@/libs/common.js';
import common from '@/libs/login.js';
import graceChecker from '@/js_sdk/graceui-dataChecker/graceChecker.js';
export default {
	data() {
		return {
			picker: [
				{
					label: '申请车辆型号', //输入框title
					name: 'carType', //传值类型
					type: 'picker', //输入框类型
					disabled: false, //是否禁用
					required: true, //是否必须填写
					value: '', //输入框的值
					text: '请选择车辆型号',
					mode: 'selector', //picker的类型
					range: [] //picker的选项集合
				},
				{
					label: '申请车牌号',
					name: 'plateNumbers',
					type: 'picker',
					required: true,
					disabled: false,
					value: '',
					text: '请选择车牌号',
					mode: 'selector', //picker的类型
					range: [] //picker的选项集合
				}
			],
			carInfoList: [],
			column: [
				{
					label: '起始日期',
					value: '',
					required: true,
					name: 'startDate',
					type: 'picker',
					mode: 'date',
					text: '请选择',
					start:format('YY-mm-dd',new Date())
				},
				
				{
					label: '起始时间',
					value: '',
					required: true,
					name: 'startTime',
					type: 'picker',
					mode: 'time',
					text: '请选择',
				},
				{
					label: '结束日期',
					value: '',
					required: true,
					name: 'endDate',
					type: 'picker',
					mode: 'date',
					text: '请选择',
					start:format('YY-mm-dd',new Date())
				},
				{
					label: '结束时间',
					value: '',
					required: true,
					name: 'endTime',
					type: 'picker',
					mode: 'time',
					text: '请选择',
				},
				{
					label: '用车原因',
					name: 'applyReason',
					type: 'normalInput',
					disabled: false,
					required: true,
					value: ''
				},
				{
					label: '附件(最多上传三张)',
					type: 'upload',
					url: '/stu/common/uploadFile',
					docFileId: '',
					docFileUrl: '',
					attachmentVoListStr: '',
					limitSize: 3,
					disabled: false
				}
			],
			rule: [
				{
					name: 'carType',
					checkType: 'string',
					checkRule: '1,99',
					errorMsg: '请选择车型'
				},
				{
					name: 'plateNumbers',
					checkType: 'string',
					checkRule: '1,99',
					errorMsg: '请选择车牌号'
				},
				{
					name: 'startDate',
					checkType: 'string',
					checkRule: '10,10',
					errorMsg: '请选择起始日期'
				},
				{
					name: 'endDate',
					checkType: 'string',
					checkRule: '10,10',
					errorMsg: '请选择结束日期'
				},
				{
					name: 'startTime',
					checkType: 'string',
					checkRule: '5,5',
					errorMsg: '请选择起始时间'
				},
				{
					name: 'endTime',
					checkType: 'string',
					checkRule: '5,5',
					errorMsg: '请选择结束时间'
				},
				{
					name: 'applyReason',
					checkType: 'string',
					checkRule: '1,100',
					errorMsg: '请填写用车原因'
				},
			]
		};
	},
	async onLoad() {
		this.getCarList();
	},
	methods: {
		//查询可用车辆列表
		async getCarList() {
			let resp = await this.$http.post({
				url: '/oa/carConfig/list'
			});
			this.carInfoList = resp.result;
			if (resp.code == 0) {
				this.picker[0].range = resp.result.map(item => {
					return {
						id: item.id,
						name: item.carType
					};
				});
				this.picker[1].range = resp.result.map(item => {
					return {
						id: item.id,
						name: item.plateNumbers
					};
				});
			}
		},
		async pickerChange(e) {
			this.carInfoList.forEach(item => {
				if (item.id == e.value) {
					this.picker[0].text = item.carType;
					this.picker[0].value = item.id;
					this.picker[1].text = item.plateNumbers;
					this.picker[1].value = item.id;
				}
			});
		},
		//提交或修改请假申请
		async submitFun() {
			let params = {
				carId: this.picker[0].value,
				startTime: `${this.column[0].text} ${this.column[1].text}`,
				endTime: `${this.column[2].text} ${this.column[3].text}`,
				applyReason: this.column[4].value,
				attachmentVoListStr: this.column[5].attachmentVoListStr, //附件列表
			};
			if(params.endTime<params.startTime) {
				uni.showToast({
					title:"开始时间不能大于结束时间！",
					icon:"none"
				})
				return
			};
			let formData = this.picker.concat(this.column);
			let checkObj = {};
			formData.map(item => {
				let name = item.name
				let value = item.value
				checkObj[name]=value
			});
			let checkRes = graceChecker.check(checkObj, this.rule);
			if (checkRes) {
				this.$http
					.post({
						url: '/oa/carApply/save',
						data: params
					})
					.then(e => {
						this.showToast('提交成功',()=> {uni.navigateBack()});
					})
			} else {
				uni.showToast({
					title: graceChecker.error,
					icon: 'none'
				});
			}
		},
	},
	components: {
		baseForm,
		pickerModule
	},
	watch: {}
};
</script>

<style></style>
